//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import { invoke } from "@tauri-apps/api/core";

export type BasicBoardInfo = {
    board_name: string;
};

export type UserPerm = {
    can_update: boolean;
    can_remove: boolean;
};

export type NodePanelSetting = {
    show_svg_node: boolean;
    show_image_node: boolean;
    show_link_node: boolean;
    show_develop: boolean;
};

export type BasicLlmInfo = {
    name: string;
    api_key: string;
    api_addr: string;
    system_prompt: string;
    default_model_name: string;
}

export type LlmInfo = {
    llm_id: string;
    basic_info: BasicLlmInfo;
    create_time: number;
    update_time: number;
}

export type BoardInfo = {
    board_id: string;
    basic_info: BasicBoardInfo;
    node_panel_setting: NodePanelSetting;
    image_fs_id: string;
    llm_list: LlmInfo[];
    create_time: number;
    update_time: number;
    user_perm: UserPerm;
};

export type CreateRequest = {
    session_id: string;
    basic_info: BasicBoardInfo;
};

export type CreateResponse = {
    code: number;
    err_msg: string;
    board_id: string;
};

export type UpdateBasicRequest = {
    session_id: string;
    board_id: string;
    basic_info: BasicBoardInfo;
};

export type UpdateBasicResponse = {
    code: number;
    err_msg: string;
};

export type UpdateNodePanelSettingRequest = {
    session_id: string;
    board_id: string;
    node_panel_setting: NodePanelSetting;
};

export type UpdateNodePanelSettingResponse = {
    code: number;
    err_msg: string;
};

export type RemoveRequest = {
    session_id: string;
    board_id: string;
};

export type RemoveResponse = {
    code: number;
    err_msg: string;
};

export type ListMyRequest = {
    session_id: string;
};

export type ListMyResponse = {
    code: number;
    err_msg: string;
    board_list: BoardInfo[];
};

export type ListByTeamRequest = {
    session_id: string;
};

export type ListByTeamResponse = {
    code: number;
    err_msg: string;
    board_list: BoardInfo[];
};

export type GetRequest = {
    session_id: string;
    board_id: string;
};

export type GetResponse = {
    code: number;
    err_msg: string;
    board: BoardInfo;
};

export type AddLlmRequest = {
    session_id: string;
    board_id: string;
    basic_info: BasicLlmInfo;
};

export type AddLlmResponse = {
    code: number;
    err_msg: string;
    llm_id: string;
};

export type UpdateLlmRequest = {
    session_id: string;
    board_id: string;
    llm_id: string;
    basic_info: BasicLlmInfo;
};

export type UpdateLlmResponse = {
    code: number;
    err_msg: string;
};

export type RemoveLlmRequest = {
    session_id: string;
    board_id: string;
    llm_id: string;
};

export type RemoveLlmResponse = {
    code: number;
    err_msg: string;
};

//创建白板
export async function create(request: CreateRequest): Promise<CreateResponse> {
    const cmd = 'board_api_cmds_create';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<CreateResponse>(cmd, {
        request,
    });
}

//更新白板基本信息
export async function update_basic(request: UpdateBasicRequest): Promise<UpdateBasicResponse> {
    const cmd = 'board_api_cmds_update_basic';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateBasicResponse>(cmd, {
        request,
    });
}

//更新面板设置
export async function update_node_panel_setting(request: UpdateNodePanelSettingRequest): Promise<UpdateNodePanelSettingResponse> {
    const cmd = 'board_api_cmds_update_node_panel_setting';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateNodePanelSettingResponse>(cmd, {
        request,
    });
}

//删除白板
export async function remove(request: RemoveRequest): Promise<RemoveResponse> {
    const cmd = 'board_api_cmds_remove';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveResponse>(cmd, {
        request,
    });
}

//列出我的白板
export async function list_my(request: ListMyRequest): Promise<ListMyResponse> {
    const cmd = 'board_api_cmds_list_my';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListMyResponse>(cmd, {
        request,
    });
}

//列出团队下的白板
export async function list_by_team(request: ListByTeamRequest): Promise<ListByTeamResponse> {
    const cmd = 'board_api_cmds_list_by_team';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListByTeamResponse>(cmd, {
        request,
    });
}

//获取单个白板
export async function get(request: GetRequest): Promise<GetResponse> {
    const cmd = 'board_api_cmds_get';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<GetResponse>(cmd, {
        request,
    });
}

// 增加llm
export async function add_llm(request: AddLlmRequest): Promise<AddLlmResponse> {
    const cmd = 'board_api_cmds_add_llm';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<AddLlmResponse>(cmd, {
        request,
    });
}

// 更新llm
export async function update_llm(request: UpdateLlmRequest): Promise<UpdateLlmResponse> {
    const cmd = 'board_api_cmds_update_llm';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateLlmResponse>(cmd, {
        request,
    });
}

// 删除llm
export async function remove_llm(request: RemoveLlmRequest): Promise<RemoveLlmResponse> {
    const cmd = 'board_api_cmds_remove_llm';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveLlmResponse>(cmd, {
        request,
    });
}